import unittest

from mudpyl.redirector import database
from mudpyl.redirector.database import Color,String

import time

class DataBaseTest(unittest.TestCase):
    
    def setUp(self):

        database.init()

        self.engine=database.engine
        self.session=database.Session()
        self.Cquery=self.session.query(Color)
        self.Squery=self.session.query(String)

    def test_create_color(self):

        color=Color('RED','BLACK',bold=False)

        self.session.add(color)
        self.assertEqual(self.session.query(Color).filter(Color.fore=='RED').first().fore,color.fore)

    def test_create_String(self):
        color=Color('GREEN','BLACK',bold=False)
        self.session.add(color)
        
        nowtime=time.time()

        string=String(content='hello',color=color)
        self.session.add(string)
        self.session.commit()

        data_string=self.Squery.first()
        stime=data_string.time
        print data_string.colored()
        self.assertTrue(stime-nowtime<1)
        self.assertEqual(data_string.content,'hello')


    
if __name__=='__main__':
    unittest.main()

